package org.example.sec11.interceptors.client;

import io.grpc.*;

import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class DealLineInterceptor implements ClientInterceptor {
    private final Duration duration;

    public DealLineInterceptor(Duration duration) {
        this.duration = duration;
    }

    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel channel) {
        if (Objects.isNull(callOptions.getDeadline())) {
            callOptions = callOptions.withDeadline(Deadline.after(duration.toMillis(), TimeUnit.MILLISECONDS));
        }
        return channel.newCall(method, callOptions);
    }
}
